<?
//做啥上传图片
$boundary = uniqid('------------------');

$m_line="\r\n";

/*
$t = array();

$t[] = "--".$boundray;
$t[] = "Content-Disposition: form-data; name=\"img\"; filename=\"".'image.jpg'."\"";
$t[] = "Content-Type: image/jpeg".$m_line.$m_line;   
$c = implode($m_line,$t); 
$file = 'H:/test/shop/img/admin/login-bg.jpg';
$c .= file_get_contents($file);
$c .=$m_line."--".$boundray;    
*/

$file = 'H:/test/shop/img/admin/login-bg.jpg';
$arr = array(
	'status'=>'图片上传测试2'.time(),
	'pic'=>file_get_contents($file),
);


$url = 'http://api.zuosa.com/statuses/update.json';
// create a new cURL resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-Type:multipart/form-data;boundary=$boundary"));

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch,CURLOPT_USERPWD, 'falconchen:7340985');
$post_data = build_http_query_multi($arr);

var_dump($post_data);

curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$rsp = curl_exec($ch);   

var_dump( json_decode($rsp));



function build_http_query_multi($params) {
     
      
        uksort($params, 'strcmp'); 
        
        $boundary = uniqid('------------------');
		$MPboundary = '--'.$boundary;
		$endMPboundary = $MPboundary. '--';
		$multipartbody = '';

        foreach ($params as $parameter => $value) { 
            
        //if( $parameter == 'pic' && $value{0} == '@' )
        if( in_array($parameter,array("pic","image")) )
        {
        	$content = $value;
        	$filename = 'pic.jpg';
        	$mime ='image/jpeg'; 
        	
        	$multipartbody .= $MPboundary . "\r\n";
			$multipartbody .= 'Content-Disposition: form-data; name="' . $parameter . '"; filename="' . $filename . '"'. "\r\n";
			$multipartbody .= 'Content-Type: '. $mime . "\r\n\r\n";
			$multipartbody .= $content. "\r\n";
        }
        else
        {
        	$multipartbody .= $MPboundary . "\r\n";
			$multipartbody .= 'content-disposition: form-data; name="'.$parameter."\"\r\n\r\n";
			$multipartbody .= $value."\r\n";
			
        }    
            
            
           
             
        } 
        
        $multipartbody .=  $endMPboundary."\r\n";

        return $multipartbody; 
	}
	